package think.in.java.concurrency.practise.exec3;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Created by IntelliJ IDEA.
 * User: Richard
 * Date: 5/19/11
 * Time: 12:20 AM
 * a example for future and the Executor for the multi threads
 */

public class MainThread {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        //create a thread pool
        ExecutorService executorService = Executors.newCachedThreadPool();
        List<Future<Integer>> results = new ArrayList<Future<Integer>>();
        for(int i =0 ; i<10;i++){
            // put the callable object into the thread pool
            Future<Integer> call = executorService.submit(new ForFuture());
            results.add(call);

        }
        while(results.size()!=0){
            for(int i =0 ; i<results.size() ; i++){
                if(results.get(i).isDone()){
                    System.out.println("the " +results.get(i).get()+" is finshed!" );
                    results.remove(i);
                }
                System.out.println("Waiting!!");
                Thread.sleep(100);
            }
        }
    }
}
